/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//check cai coi
//cho nhan cho nguyen
package caro;


import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author PHUC TRI
 */
public class Map {
   private int size;
   private int[][] map;
    //1: quan minh
    //0: null
    //-1: quan dich
   public void init(int s){
        size=s;
        map=new int[size][size];
        
    }
   public void set(int x,int y,int value){
        map[x][y]=value;
    }
   public void unSet(int x,int y){
        map[x][y]=0;
    }
   public int eval(int x,int y,int player){
        map[x][y]=player;
        int result=0;
        int e1,e2;
        for(int i=y;i>y-5;i--){
            if(i<0 || map[x][i]==-player) break;
            e1=0;
            e2=0;
            for(int j=i+4;j>=i;j--){
                if(j>=size || map[x][j]==-player){
                    e1=0;
                    break;
                }
                
                if(map[x][j]==player) e2+=player;
                else e2=0;
                e1+=e2;
            }
            result+=e1;
        }
        //System.out.println(result);
        for(int i=x;i>x-5;i--){
            if(i<0 || map[i][y]==-player) break;
            e1=0;
            e2=0;
            for(int j=i+4;j>=i;j--){
                if(j>=size || map[j][y]==-player){
                    e1=0;
                    break;
                }
                if(map[j][y]==player) e2+=player;
                else e2=0;
                e1+=e2;
            }
            result+=e1;
        }
        //System.out.println(result);
        int i=x,j=y;
        while(i>x-5){
            if(i<0 || j<0 || map[i][j]==-player) break;
            int a=i+4,b=j+4;
            e1=0;
            e2=0;
            while(a>=i){
                if(a>=size ||b>=size || map[a][b]==-player){
                    e1=0;
                    break;
                }
                if(map[a][b]==player) e2+=player;
                else e2=0;
                e1+=e2;
                a--;
                b--;
            }
            result+=e1;
            i--;
            j--;
        }
        //System.out.println(result);
        i=x;
        j=y;
        while(i<x+5){
            if(i>=size || j<0 || map[i][j]==-player) break;
            int a=i-4,b=j+4;
            e1=0;
            e2=0;
            while(a<=i){
                if(a<0 ||b>=size || map[a][b]==-player){
                    e1=0;
                    break;
                }
                if(map[a][b]==player) e2+=player;
                else e2=0;
                e1+=e2;
                a++;
                b--;
            }
            result+=e1;
            i++;
            j--;
        }
        map[x][y]=0;
      
    //System.out.println("Result at point: "+x+" "+y+ ":   "+result);
      return (result>0? result:-result);   
    }
    
   public List<PointMap> findPointAvailMap() {
       List<PointMap> result = new ArrayList<PointMap>();
       for (int i=0; i<size; i++) {
           for (int j=0; j<size; j++) {
               if (map[i][j] == 0) {
                   result.add(new PointMap(i,j)) ;
               }
           }
       }
       return result;
   }
   public void printPointIsCheck() {
       for (int i=0; i<size;i++) {
           for (int j=0;j<size;j++) {
               if (map[i][j]!=0) {
                   System.out.print("("+i+","+j+","+map[i][j]+")");
                   System.out.print("   ");                   
               }               
           }
       }
       System.out.println();
   }
  
   
}
